import { Space } from 'ant-design-vue'
import { defineComponent, ref } from 'vue'

import { cinemaApi, orderApi } from '@/apis'
import { type ProTableProps, type ProTableRef, ProTable } from '@/components'
import { TABLE_ITEM_RENDER, TABLE_RENDERER } from '@/constants'
import { provinceOptions } from '@/views/order/constants'

export default defineComponent({
  name: 'DyCinemaManage',
  setup() {
    const cityList = ref([] as any[])
    const tableRef = ref({} as ProTableRef)
    const tableConfig = ref({
      fixedHeight: true,
      options: {
        border: true,
        align: 'center',
        formConfig: {
          items: [
            {
              title: '影院名称',
              field: 'name',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入影院名称',
                  allowClear: true
                }
              }
            },

            {
              title: '所在位置',
              field: 'province',
              itemRender: {
                name: TABLE_ITEM_RENDER.ASelect,
                props: {
                  placeholder: '省',
                  allowClear: true
                },
                events: {
                  change: handleProvinceChange
                },
                options: provinceOptions
              }
            },
            {
              field: 'city',
              itemRender: {
                name: TABLE_ITEM_RENDER.ASelect,
                props: {
                  placeholder: '市',
                  allowClear: true
                },
                options: cityList
              }
            },
            {
              itemRender: {
                name: TABLE_RENDERER.FormItemBtns
              }
            }
          ]
        },
        columns: [
          {
            title: '影院ID',
            field: 'cinemaId',
            minWidth: 100
          },
          {
            title: '影院名称',
            field: 'cinemaName',
            minWidth: 150
          },
          {
            title: '所在省市',
            minWidth: 150,
            slots: {
              default: 'provinceRow'
            }
          },
          {
            title: '详细地址',
            field: 'cinemaAddress',
            minWidth: 200
          },
          {
            title: '门店电话',
            minWidth: 150,
            field: 'cinemaPhone'
          }
        ],
        pagerConfig: {
          pageSize: 15
        },
        proxyConfig: {
          form: true,
          props: {
            result: 'dataList',
            total: 'page.totalCount'
          },
          ajax: {
            query: async ({ page, form }) => {
              if (form.city) {
                form.city = form.city.replace('市', '')
              }
              const data = await cinemaApi.getCinemaList({
                pageParameter: {
                  pageSize: page.pageSize,
                  currentPage: page.currentPage
                },
                ...form
              })
              return data
            }
          }
        }
      }
    } as ProTableProps)

    function handleProvinceChange(value) {
      const provinceData = provinceOptions.filter(val => {
        return val.value == value.data.province
      })
      cityList.value = provinceData[0].children ? provinceData[0].children : []
    }

    return {
      tableRef,
      tableConfig
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            provinceRow: ({ row }) => (
              <Space>
                <span>{`${row.province ? row.province : ''}${
                  row.city ? row.city : ''
                }`}</span>
              </Space>
            ),
            timeRow: ({ row }) => (
              <Space>
                <span>
                  {row.openTime ? `${row.openTime} - ${row.closeTime}` : ''}
                </span>
              </Space>
            )
          }}
          {...this.tableConfig}
        />
      </div>
    )
  }
})
